/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Place;
/**
 * View for creating new Places
 *
 */
public class NewPlaceView extends Form implements CommandListener{
	private ApplicationController controller = null;
	private TextField nameField = null;
	private TextField addressField = null;
	private TextField cityField = null;
	private TextField phoneField = null;
	private TextField latField = null;
	private TextField lngField = null;
	
	private int placeId; //if a place is loaded for editing
	private final int MAX_LENGTH = 500;
	private Command submit;
	private Command geocode;
	
	public NewPlaceView(String title, ApplicationController mpsMain) {
		super(title);
		this.controller = mpsMain;
		initialize();
		createCommands();
		this.setCommandListener(this);
	}

	/**
	 * Creates the commands
	 */
	private void createCommands() {
		submit = new Command("Send", "Send Place", Command.SCREEN, 0);
		geocode = new Command("Geocode", "Geocode Place", Command.SCREEN,0);
		this.addCommand(submit);
		this.addCommand(geocode);
	}
	
	/**
	 * Initializes the visual components
	 *
	 */
	private void initialize(){
		StringItem text = new StringItem("Create a new Place", null);
		nameField = new TextField("Name", null, 90,0);
		addressField = new TextField("Address", null, 100,0);
		cityField = new TextField("City", null, 90,0);
		phoneField = new TextField("Phone", null, 90,0); //TODO: restrict size??
		latField = new TextField("Latitude", null, 20, TextField.UNEDITABLE);
		lngField = new TextField("Longitude", null, 20, TextField.UNEDITABLE);
		

		this.append(text);
		this.append(nameField);
		this.append(addressField);
		this.append(cityField);
		this.append(phoneField);
		this.append(latField);
		this.append(lngField);
	}

	/**
	 * Action handler
	 */
	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType() == Command.BACK){
			ScreenController.getInstance().last();
		}else if(c == submit){
			controller.submitPlace();
		}else if(c == geocode){
			controller.geocodePlace();
		}
	}
	
	/**
	 * loads the place into the form
	 * @param p = Place object containing data
	 */
	public void loadPlace(Place p){
		nameField.setString(p.getName());
		addressField.setString(p.getAddress());
		cityField.setString(p.getCity());
		phoneField.setString(p.getPhone());
		
		placeId = p.getId();
	}
	
	/*
	 * Getters and Setters
	 */

	public int getPlaceId() {
		return placeId;
	}

	public void setPlaceId(int placeId) {
		this.placeId = placeId;
	}

	public TextField getNameField() {
		return nameField;
	}

	public void setNameField(TextField nameField) {
		this.nameField = nameField;
	}

	public TextField getAddressField() {
		return addressField;
	}

	public void setAddressField(TextField addressField) {
		this.addressField = addressField;
	}

	public TextField getCityField() {
		return cityField;
	}

	public void setCityField(TextField cityField) {
		this.cityField = cityField;
	}

	public TextField getPhoneField() {
		return phoneField;
	}

	public void setPhoneField(TextField phoneField) {
		this.phoneField = phoneField;
	}

	public TextField getLatField() {
		return latField;
	}

	public void setLatField(TextField latField) {
		this.latField = latField;
	}

	public TextField getLngField() {
		return lngField;
	}

	public void setLngField(TextField lngField) {
		this.lngField = lngField;
	}
	
}
